//
//  DisplayUserViewController.m
//  Closer
//
//  Created by zhangkai on 16/3/10.
//  Copyright © 2016年 Closer_All. All rights reserved.
//

#import "DisplayUserViewController.h"
#import <EaseMobSDKFull/EaseMob.h>
#import "ChatViewController.h"
#import "AddfriendViewController.h"
#import "ShowGroupViewController.h"
#import "CreatGroupViewController.h"

#define cell_id @"cell_id"
@interface DisplayUserViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)addFrienddButton:(UIButton *)sender;
- (IBAction)createGroup:(id)sender;

@property(nonatomic,strong)NSArray   *userDataArray;
@property(nonatomic,strong)NSArray  *groupDataArray;

@end
@implementation DisplayUserViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=YES;
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.userDataArray=[[NSArray alloc]init];
    
    [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
        if (!error) {
            NSLog(@"获取成功 -- %@",buddyList);
            self.userDataArray=buddyList;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
            });
        }
    } onQueue:nil];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.userDataArray=nil;
}
#pragma mark - tableview代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0){
        return 3;
    }
    if (section==1) {
        return self.userDataArray.count;
    }
    return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cell_id];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
    }
    if (indexPath.section==0){
        switch (indexPath.row) {
            case 0:
                 cell.textLabel.text=@"申请与通知";
                break;
            case 1:
                 cell.textLabel.text=@"群组";
                break;
            case 2:
                 cell.textLabel.text=@"聊天室";
                break;
                
            default:
                break;
        }
    }
    if (indexPath.section==1) {
        EMBuddy *buddy=self.userDataArray[indexPath.row];
        cell.textLabel.text=buddy.username;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        if (indexPath.row==1) {
            ShowGroupViewController *showGroupController=[[ShowGroupViewController alloc]init];
             [self presentViewController:showGroupController animated:YES completion:^{
                 
             }];
        }
        return;
    }
    if (indexPath.section==1) {
     
      EMBuddy  *buddy=  self.userDataArray[indexPath.row];
        ChatViewController *chatController=[[ChatViewController alloc]initWithChatter:buddy.username isGroup:NO];
        [self presentViewController:chatController animated:YES completion:^{
            
        }];
    }
}
- (IBAction)addFrienddButton:(UIButton *)sender {
    AddfriendViewController *addfriendConreoller=[[AddfriendViewController alloc]init];
    [self presentViewController:addfriendConreoller animated:YES completion:^{
        
    }];
}
- (IBAction)createGroup:(id)sender {
    CreatGroupViewController *creatController=[[CreatGroupViewController alloc]initWithNibName:@"CreatGroupViewController" bundle:nil];
    [self presentViewController:creatController animated:YES completion:^{
        
    }];
}
-(void)dealloc{
    self.userDataArray=nil;
}

@end
